jsx3.lang.Package.definePackage("ux.megatron.gui", function(ux) {
});

jsx3.Class.defineClass(
        "ux.megatron.gui.RSSViewer",
        jsx3.gui.Block,
        null,
        function (block) {

            block.prototype.init = function(strName, vntLeft, vntTop, vntWidth, vntHeight, strHTML) {
                this.jsxsuper(strName, vntLeft, vntTop, vntWidth, vntHeight, strHTML);
            }
    
            block.prototype.setFeedURL = function(feedURL) {
                this.feedURL = feedURL;
            }
            
            block.prototype.getFeedURL = function() {
                return this.feedURL;
            }

            block.prototype.setLoadingText = function(text) {
                this.loadingText = text;
            }
            
            block.prototype.getLoadingText = function() {
                return this.loadingText;
            }
            
            block.prototype.setTimeout = function(timeout) {
                this.timeout = timeout;
            }
            
            block.prototype.getTimeout = function() {
                return parseInt(this.timeout) || 10000;
            }
            
            block.prototype.setTimeoutText = function(text) {
                this.timeoutText = text;
            }
            
            block.prototype.getTimeoutText = function() {
                return this.timeoutText;
            }
            
            block.prototype.onAfterPaint = function() {
                if(this.getFeedURL() && !jsx3.ide) {
                    this.setFeedURL(ux.megatron.Main.getServer().resolveURI("jsxaddin://user!cdbadmin/docs/RSSViewer.xml"));
                    this.setText("<img style='float:left; margin:0 5 0 0;' src='"+ux.megatron.Main.getServer().resolveURI("jsxaddin://user!Megatron/images/loading.gif")+"'/>"+ this.getLoadingText(),true);
                    var req = new jsx3.net.Request();
                    req.open("GET", this.getFeedURL(), true);
                    req.subscribe(jsx3.net.Request.EVENT_ON_RESPONSE, this, this.onGetRSSResponse);
                    req.subscribe(jsx3.net.Request.EVENT_ON_TIMEOUT, this, this.onGetRSSTimeout);
                    req.send(null, this.getTimeout());
                }
            }

            block.prototype.onGetRSSResponse = function(objEvent) {
                var req = objEvent.target;
                var rss = req.getResponseXML();
                this.parseRSS(rss);
            }
            
            block.prototype.parseRSS = function(rss) {
                var items = rss.selectNodes("//item").iterator();
                var arrItems = [];
                while (items.hasNext()) {
                    var item = items.next();
                    var title = item.selectSingleNode("title").getValue();
                    var link = item.selectSingleNode("link").getValue();
                    arrItems.push("<li><a style='color:#000000;text-decoration:none;' href='"+link+"' target='_blank'>"+title+"</a></li>");
                }
                this.setText("<ul style='list-style: disc inside none; margin:0px; padding:0px;'>"+arrItems.join("")+"</ul>",true);
            }

            block.prototype.onGetRSSTimeout = function(objEvent) {
                this.setText(this.getTimeoutText(), true);
            }

        }
        )